import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import { productid } from '../../atom/config';
import refresh from '@dfeidao/atom-web/url/refresh';
import get from '@dfeidao/atom-web/local/get';


export default async function a003(fd: IFeidaoAiBrowserComponent) {
	// 保存
	const no = get(fd, 'no');
	console.log('zj9a003no', no);

	const equipment_kind_no = fd.data.node.querySelector<HTMLInputElement>('[data-id="equipment_kind_no"]').value;

	const equipment_kind_name = fd.data.node.querySelector<HTMLInputElement>('[data-id="equipment_kind_name"]').value;
	if (!equipment_kind_name) {
		alert("请填写设备种类名称");
		return;
	}

	const res1 = await nodejs<Array<{ equipment_kind_name: string }>>('device-type/zj-000009/s003', {
		filter: {
			equip_type_no: no
		}
	});
	const arr: String[] = [];
	res1.forEach((v) => {
		arr.push(v.equipment_kind_name);
	});
	console.log('zj9a003+no', arr);

	for (let i = 0; i < arr.length; i++) {
		if (equipment_kind_name === arr[i]) {
			alert("设备种类名称不能重复！");
			return;
		}
	}


	const res = await nodejs<{ code: 0 | 1; result: string }>('device-type/zj-000009/s002', {
		filter: {
			productid,
			equipment_kind_no: equipment_kind_no,
			equipment_kind_name: equipment_kind_name
		}
	});
	// console.log(res.code);
	if (res.code === 1) {
		alert('修改成功');
		refresh();
	} else {
		alert('修改失败');
		refresh();
	}
}
